﻿#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace UFrame
{
    public class TestAutoUISys : BaseTestWindow
    {
        public float invertTime = 0.3f;

        public string text = "";

        private Dictionary<int, string> textValDic = new Dictionary<int, string>();

        [MenuItem("Plateface/UGUI/测试UI")]
        static void ShowEditor()
        {
            TestAutoUISys editor = GetWindow<TestAutoUISys>();
            editor.Show(true);
        }

        private void OnGUI()
        {
            invertTime = EditorGUILayout.FloatField("测试间隔时间", invertTime);
            text = EditorGUILayout.TextField("text测试", text);

            if (GUILayout.Button("测试UI"))
            {
                var go = Selection.activeGameObject;
                if (go == null)
                {
                    return;
                }

                var ui = go.GetComponent<IBaseAutoTest>();
                if (ui == null)
                {
                    Debug.Log("UI不支持测试!!!");
                    return;
                }

                //支持测试
                string fileName = go.name;
                string newPath = string.Format(DefineAutoTest.path, fileName);
                string uiPath = Application.dataPath + newPath;

                if (!File.Exists(uiPath))
                {
                    Debug.LogError("测试文件不存在");
                    return;
                }

                string json = File.ReadAllText(newPath, Encoding.Unicode);
                UITestGroupJson uiGroup = JsonUtility.FromJson<UITestGroupJson>(json);
                GameObject uiRoot = GameObject.Find(uiGroup.UIName);
                AutoTestUI.I.currentUI = uiRoot;
            }

            if (GUILayout.Button("取消测试"))
            {
                AutoTestUI.I.currentUI = null;
            }

            if (GUILayout.Button("对选中节点下的text测试"))
            {
                textValDic.Clear();
                GameObject go = Selection.activeGameObject;
                if (go != null)
                {
                    Text[] tAry = go.GetComponentsInChildren<Text>();
                    foreach (var item in tAry)
                    {
                        textValDic.Add(item.GetInstanceID(), item.text);
                        item.text = text;
                    }
                }
            }

            if (GUILayout.Button("还原text"))
            {
                GameObject go = Selection.activeGameObject;
                if (go != null)
                {
                    Text[] tAry = go.GetComponentsInChildren<Text>();
                    foreach (var item in tAry)
                    {
                        int guid = item.GetInstanceID();
                        if (textValDic.ContainsKey(guid))
                        {
                            item.text = textValDic[guid];
                        }
                    }
                }
            }
        }

        public void Update()
        {
            AutoTestUI.I.operationTime += Time.deltaTime;
            if (AutoTestUI.I.operationTime >= invertTime)
            {
                AutoTestUI.I.TestCurrentUI();
                AutoTestUI.I.operationTime -= invertTime;
            }

            //Debug.Log("Update" + Time.deltaTime);
        }
    }
}
#endif